package com.abel.roommanager.controller;
import javax.annotation.Resource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.abel.roommanager.pojo.User;
import com.abel.roommanager.service.UserService;
@Controller
@RequestMapping("/user")
@Scope("prototype")
public class UserController {
	@Resource
	private UserService  userService;
	@RequestMapping("/login")
	@ResponseBody
  public int login(User user,Model model) {
//		获取Subject对象
		Subject currentUser=SecurityUtils.getSubject();
//		判断当前用户是否已经认证
		int re=0;
	     if (!currentUser.isAuthenticated()) {
			UsernamePasswordToken token=new UsernamePasswordToken(user.getUsername(), user.getPassword());
			try {
				currentUser.login(token);
			} catch (UnknownAccountException e) {	
			  re=1;
			}catch (IncorrectCredentialsException e) {
				re=2;
			}catch (LockedAccountException e) {
				re=3;
			}catch (AuthenticationException e) {
				re=4;
			}
		}
	return re;
    }
}
